
package com.way.filemanager.activities;

import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.way.filemanager.R;
import com.way.filemanager.util.DialogHelper;
import com.way.filemanager.util.MediaHelper;
import com.way.filemanager.util.MimeTypeHelper;

import java.io.File;

import hwdroid.app.HWActivity;

public class GlobalSearchActivity extends HWActivity {
    private static final String TAG = "GlobalSearchActivity";

    @Override
    protected void onCreate(Bundle state) {
        super.onCreate(state);
        Log.v(TAG, "FileManagerAppFrame oncreate");

        Intent temp = getIntent();
        if (Intent.ACTION_VIEW.equals(temp.getAction())) {
            Log.v(TAG, "getAction:" + getIntent().getAction());
            Log.v(TAG, "getDataString:" + getIntent().getDataString());
            Log.v(TAG, "getData:" + getIntent().getData());
            Log.v(TAG, "getExtras:" + getIntent().getExtras());
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);

            // Obtain the mime/type and passed it to intent
            String mime = MimeTypeHelper.getMimeType(this, getIntent().getDataString());
            File file = new File(getIntent().getDataString());
            if (!file.exists()) {
                return;
            }
            if (mime != null) {
                intent.setDataAndType(getUriFromFile(this, file), mime);
            } else {
                intent.setData(getUriFromFile(this, file));
            }

            // Resolve the intent
            /*
             * resolveIntent(ctx, intent, choose, createInternalIntents(ctx,
             * fso), 0, R.string.associations_dialog_openwith_title,
             * R.string.associations_dialog_openwith_action, true,
             * onCancelListener, onDismissListener);
             */
            try{
            startActivity(intent);
            }
            catch(ActivityNotFoundException e)
            {
                DialogHelper.showToast(this, R.string.ali_openfile_error,
                      Toast.LENGTH_SHORT);
            }
        }
        finish();
    }

    private static Uri getUriFromFile(Context ctx, File file) {
        ContentResolver cr = ctx.getContentResolver();
        Uri uri = MediaHelper.fileToContentUri(cr, file);
        if (uri == null) {
            uri = Uri.fromFile(file);
        }
        return uri;
    }
}
